/*关于路由状态的存储*/
import { defineStore } from "pinia";

export const useRouterStore = defineStore("routes", {
    state: () => ({
        routes: []
    }),
    actions: {
        setRoutes(routes) {
            // 排序函数，用于按 title 的字数排序
            const sortByTitleLength = (a, b) => {
                const titleLengthA = (a.meta.title || '').length; // 获取 title 字符串的长度
                const titleLengthB = (b.meta.title || '').length;
                return titleLengthA - titleLengthB; // 按照字数升序排序
            };

            // 对父级路由进行排序，根据 title 字符数排序
            this.routes = routes.sort(sortByTitleLength);
        },
        clearRoutes() {
            this.routes = [];
        }
    },
    persist: {
        enable: true,
        strategise: [
            {
                storage: localStorage, // 持久化到本地存储
                paths: ['routes'] // 指定需要持久化的字段
            }
        ]
    }
})
